package com.example.util;

import ch.qos.logback.classic.Logger;
import ch.qos.logback.classic.LoggerContext;
import ch.qos.logback.classic.sift.SiftingAppender;
import ch.qos.logback.classic.spi.ILoggingEvent;
import ch.qos.logback.core.sift.AppenderTracker;
import org.apache.commons.codec.DecoderException;
import org.apache.commons.codec.binary.Hex;
import org.slf4j.LoggerFactory;

import java.nio.ByteBuffer;

public class StringUtil {
    public StringUtil() {
    }

    public static String getHexStr(byte[] chBuf) {
        String str = "";
        byte[] var5 = chBuf;
        int var4 = chBuf.length;

        for(int var3 = 0; var3 < var4; ++var3) {
            byte b = var5[var3];
            str = str + String.format("%02X", b);
        }

        return str;
    }

    public static String getHexStr(String format, byte[] chBuf) {
        String str = "";
        byte[] var6 = chBuf;
        int var5 = chBuf.length;

        for(int var4 = 0; var4 < var5; ++var4) {
            byte b = var6[var4];
            str = str + String.format(format, b);
        }

        return str;
    }

    public static void removeappnder(String s) {
        LoggerContext loggerContext = (LoggerContext) LoggerFactory.getILoggerFactory();
        Logger logeer = loggerContext.getLogger("ROOT");
        SiftingAppender ssa1 = (SiftingAppender)logeer.getAppender(s);
        AppenderTracker<ILoggingEvent> tracker = ssa1.getAppenderTracker();
        tracker.removeStaleComponents(System.currentTimeMillis() + 1800000L);
    }

    public static byte[] toBytes(short value) {
        ByteBuffer buffer = ByteBuffer.allocate(2);
        buffer.putShort(value);
        buffer.flip();
        return buffer.array();
    }

    public static byte[] decodeHex(String hexstr) throws Exception {
        Hex hex = new Hex();
        Object var2 = null;

        try {
            byte[] bytes = hex.decode(hexstr.getBytes());
            return bytes;
        } catch (DecoderException var4) {
            throw new Exception("产生mac 错误", var4);
        }
    }
}
